﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssetMetaDataMapper
    {
        internal static IMetaData ToBusinessObject(T_AssetMetaData entity)
        {
            //IAssetMetaData obj = new AssetMetaData(
            //    entity.AssetMetaData_IDPK,
            //    entity.MetaDataID_FK,
            //    entity.AssetID_FK,
            //    entity.StatusID_FK);

            // parent is causing infinite recursion: obj.Asset = AssetMapper.ToBusinessObject ( entity.T_Asset );
//            if (entity.T_MetaData != null)
                return MetaDataMapper.ToBusinessObject(entity.T_MetaKey, entity.T_MetaValue);
            //else
            //{
            //    return null;
            //}
//            if (entity.T_Status != null) obj.Status = StatusMapper.ToBusinessObject(entity.T_Status);

  //          return obj;
        }

        internal static T_AssetMetaData ToEntity (int AssetId, IMetaData v )
        {
            T_AssetMetaData entity = new T_AssetMetaData ( );

           // entity.AssetMetaData_IDPK = v.ID;
            //entity.MetaDataID_FK = v.ID;
            if (AssetId!=0)
                entity.AssetID_FK = AssetId;
            //todo: default status id
            entity.StatusID_FK = 0;

            // parent is causing infinite recursion: entity.T_Asset = AssetMapper.ToEntity ( v.Asset );
            CondeNet.AssetManager.DataProvider.Sql.Mappers.MetaDataMapper.MetadataPairing
                    mdKeyValue = MetaDataMapper.ToEntity(v);
            entity.T_MetaKey = mdKeyValue.MetaKey;
            entity.T_MetaValue = mdKeyValue.MetaValue;
            entity.MetaKeyID_FK = mdKeyValue.MetaKey.MetaKey_IDPK;
            if (mdKeyValue.MetaValue.MetaValue_IDPK!=0)
                entity.MetaValueID_FK = mdKeyValue.MetaValue.MetaValue_IDPK;
            //if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );

            return entity;
        }

        internal static IList<IMetaData> ToBusinessObjects(IList<T_AssetMetaData> dalList)
        {
            return dalList.Select(tmp => ToBusinessObject(tmp)).ToList();
        }

        internal static IList<T_AssetMetaData> ToEntities (int AssetId, IList<IMetaData> entityList )
        {
            EntitySet<T_AssetMetaData> list = new EntitySet<T_AssetMetaData>();
             entityList.Select ( tmp => ToEntity (AssetId, tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
